package com.lx.onbording.vshe.testassignment.core.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class GenericDAOImpl<E, PK extends Serializable> implements
		GenericDAO<E, PK> {
	protected Class<E> entityClass;

	@PersistenceContext
	protected EntityManager em;

	public GenericDAOImpl() {
		ParameterizedType genericSuperclass = (ParameterizedType) getClass()
				.getGenericSuperclass();
		this.entityClass = (Class<E>) genericSuperclass
				.getActualTypeArguments()[0];
	}

	public E create(E e) {
		em.persist(e);
		return e;
	}

	public E read(PK id) {
		return em.find(entityClass, id);		
	}

	public E update(E e) {
		return em.merge(e);
	}

	public void delete(E e) {
		em.merge(e);
		em.remove(e);
	}

}
